package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.viewpage.NoScrollViewPager;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerMyExtensionActivityComponent;
import com.wgts.wgts.dagger.module.MyExtensionActivityModule;
import com.wgts.wgts.localbean.ShopKeepData;
import com.wgts.wgts.mvp.presenter.IMyExtensionActivityPresenter;
import com.wgts.wgts.mvp.view.IMyExtensionActivityView;
import com.wgts.wgts.netmodle.resp.AppModleEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.MoHuGlide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 我的推广
 */
public class MyExtensionActivity extends BaseActivity implements IMyExtensionActivityView, MyExtensionFansFragment.MyExtensionFansFragmentToActivity {


    @BindView(R.id.img_circle_head)
    CircleImageView imgCircleHead;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.img_vip_statue)
    ImageView imgVipStatue;
    @BindView(R.id.tv_vip_state)
    TextView tvVipState;
    @BindView(R.id.linear_vip)
    LinearLayout linearVip;
    @BindView(R.id.img_code)
    ImageView imgCode;
    @BindView(R.id.tv_code_title)
    TextView tvCodeTitle;
    @BindView(R.id.gif_webView)
    ImageView gifWebView;
    @BindView(R.id.tv_fans)
    TextView tvFans;
    @BindView(R.id.tv_nomal_fans)
    TextView tvNomalFans;
    @BindView(R.id.view_page)
    NoScrollViewPager viewPage;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.yv_title)
    TextView yvTitle;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.linear_git_webView)
    LinearLayout linearGitWebView;
    @BindView(R.id.img_head)
    ImageView imgHead;
    @BindView(R.id.smart_refresh_layout)
    SmartRefreshLayout smartRefreshLayout;
    private ArrayList<Fragment> fragments;

    private ShopKeepData shopKeepData;

    public static final String SHOPKEEP = "SHOPKEEP";


    private QrCodeDialogFragment fragment;

    @Inject
    IMyExtensionActivityPresenter presenter;


    @Override
    public void init() {

        DaggerMyExtensionActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .myExtensionActivityModule(new MyExtensionActivityModule(this))
                .build()
                .inject(this);

        shopKeepData = getIntent().getParcelableExtra(SHOPKEEP);
        fragment = QrCodeDialogFragment.getInstance(shopKeepData.qCodeUrl, shopKeepData.shopkeeperYQCode, shopKeepData.shopAvatar, "");
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.my_extension_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

        switch (AppInfor.getInstance().getUserState()) {
            case 0:
                imgVipStatue.setImageResource(R.mipmap.ic_store_member_white);

                break;
            case 1:
                imgVipStatue.setImageResource(R.mipmap.ic_store_member_white);

                break;
            case 2:
                imgVipStatue.setImageResource(R.mipmap.ic_store_store_white);

                break;
            case 3:
                imgVipStatue.setImageResource(R.mipmap.ic_data_manager_white);

                break;
            case 4:
                imgVipStatue.setImageResource(R.mipmap.ic_data_rudder_white);

                break;
        }

        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setEnableLoadmore(true);
        smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                ((MyExtensionFansFragment)fragments.get(viewPage.getCurrentItem())).loadMore();
            }
        });


        viewPage.setScanScroll(false);

        fragments = new ArrayList<>();
        fragments.add(MyExtensionFansFragment.getInstance(0, 0, 2));
        fragments.add(MyExtensionFansFragment.getInstance(1, 0, 3));
        //fragments.add(new );

        viewPage.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        if (shopKeepData.shopAvatar != null) {
            MoHuGlide.getInstance().glideImg(this, shopKeepData.shopAvatar, imgCircleHead);
        }
        tvUserName.setText(shopKeepData.userName);


        if (shopKeepData.userState.equals("0")) {
            tvVipState.setText("内部员工");
        } else if (shopKeepData.userState.equals("1")) {
            tvVipState.setText("普通会员");
        } else if (shopKeepData.userState.equals("2")) {
            tvVipState.setText("店主");
        } else if (shopKeepData.userState.equals("3")) {
            tvVipState.setText("掌柜子");
        } else if (shopKeepData.userState.equals("4")) {
            tvVipState.setText("分舵主");
        }

        tvCode.setText(shopKeepData.shopkeeperYQCode);

        presenter.loadGif();
    }


    @OnClick({R.id.tv_fans, R.id.tv_nomal_fans, R.id.img_back, R.id.img_code, R.id.linear_git_webView})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_fans:
                viewPage.setCurrentItem(0, false);
                tvFans.setBackgroundResource(R.drawable.text_left_bg);
                tvFans.setTextColor(ContextCompat.getColor(this, R.color.whrite));
                tvNomalFans.setTextColor(ContextCompat.getColor(this, R.color.hint_6));
                tvNomalFans.setBackground(null);
                break;
            case R.id.tv_nomal_fans:
                viewPage.setCurrentItem(1, false);
                tvNomalFans.setBackgroundResource(R.drawable.text_right_bg);
                tvFans.setBackground(null);
                tvNomalFans.setTextColor(ContextCompat.getColor(this, R.color.whrite));
                tvFans.setTextColor(ContextCompat.getColor(this, R.color.hint_6));
                break;
            case R.id.img_back:
                finish();
                break;
            case R.id.img_code:
                fragment.show(getSupportFragmentManager(), "");
                break;
            case R.id.linear_git_webView:
                Intent intent = new Intent(this, UpgradeShopKeeperActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void gifLoad(List<AppModleEntity> appModleEntityList) {
        MoHuGlide.getInstance().loadGif(this, appModleEntityList.get(0).result.get(0).sourceUrl, gifWebView);
    }

    @Override
    public void onLoadFinish() {
        smartRefreshLayout.finishLoadmore();
    }
}
